package com.manli.api.service;

import com.manli.api.pojo.QueryParamsArea1;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.List;
import com.manli.api.pojo.Result;
import com.manli.api.util.ResultUtil;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import com.manli.api.pojo.QueryParams;
import com.manli.api.dao.AAreasDao;
import com.manli.api.dto.AAreas;
import org.springframework.transaction.annotation.Transactional;


@Service("aAreasService")
public class AAreasService {
    @Autowired
    AAreasDao aAreasDao;
    @Autowired
    SQLManager sqlManager;

    
    public Result queryPage(QueryParams params) {
        /**
        * sql中表名需要修改
        */
        StringBuffer sql = new StringBuffer("select * from aAreas where 1=1 ");

        SQLReady sqlReady = new SQLReady(sql.toString());

        PageQuery<AAreas> pageQuery = new PageQuery<AAreas>(params.getPage(),params.getLimit());
        PageQuery<AAreas> query =sqlManager.execute(sqlReady,AAreas.class,pageQuery);
        return ResultUtil.success(query.getList(),query.getTotalRow());
    }

    
    public Result queryAll(QueryParamsArea1 cityCode) {
        StringBuffer sql = new StringBuffer("select * from a_areas where 1=1 ");
        sql.append(StringUtils.isEmpty(cityCode.getCityCode())?"":"and city_code = " + cityCode.getCityCode());
        List<AAreas> list = aAreasDao.execute(sql.toString());
        return ResultUtil.success(list);
    }

    
    public Result getById(String code) {
        AAreas aAreas = aAreasDao.single(code);
        return ResultUtil.success(aAreas);
    }

    @Transactional
    public Result save(AAreas aAreas) {
        aAreasDao.insert(aAreas);
        return ResultUtil.success();
    }

    @Transactional
    public Result updateById(AAreas aAreas) {
        aAreasDao.updateTemplateById(aAreas);
        return ResultUtil.success();
    }
    @Transactional
	public Result updateTemplateById(AAreas aAreas) {
        aAreasDao.updateTemplateById(aAreas);
        return ResultUtil.success();
    }
    @Transactional
    public Result removeById(String code) {
        aAreasDao.deleteById(code);
        return ResultUtil.success();
    }

    @Transactional
    public Result removeByIds(List<String> ids) {
        for (String id:ids) {
            aAreasDao.deleteById(id);
        }
        return ResultUtil.success();
    }



}
